# coding=utf-8
class Solution:
    def romanToInt(self, s: str) -> int:
        romaB = {'CM': 900, 'DCCC': 800, 'DCC': 700, 'DC': 600, 'CD': 400, 'CCC': 300, 'CC': 200, 'C': 100, 'D': 500}
        romaS = {'XC': 90, 'LXXX': 80, 'LXX': 70, 'LX': 60, 'XL': 40, 'XXX': 30, 'XX': 20, 'X': 10, 'L': 50}
        romaG = {'IX': 9, 'VIII': 8, 'VII': 7, 'VI': 6, 'IV': 4, 'III': 3, 'II': 2, 'I': 1,'V': 5,}
        res = 0
        for i in range(len(s)):
            if s[i]!='M':
                res += 1000*i
                s = s[i:]
                break
            if i ==len(s)-1:
                res+=len(s)*1000
        for i in romaG:
            if i in s:
                res += romaG[i]
                s = s.replace(i, '')
                break
        for i in romaS:
            if i in s:
                res += romaS[i]
                s = s.replace(i, '')
                break
        for i in romaB:
            if i in s:
                res += romaB[i]
                s = s.replace(i, '')
                break

        return res

a = Solution()
print(a.romanToInt('MMM'))


